package com.hq.szgl.actions;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.eredlab.g4.arm.util.ArmConstants;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.datastructure.impl.BaseDto;
import org.eredlab.g4.ccl.json.JsonHelper;
import org.eredlab.g4.ccl.util.G4Constants;
import org.eredlab.g4.rif.report.fcf.FcfDataMapper;
import org.eredlab.g4.rif.report.fcf.GraphConfig;
import org.eredlab.g4.rif.report.fcf.Set;
import org.eredlab.g4.rif.web.BaseAction;
import org.eredlab.g4.rif.web.CommonActionForm;

import com.hq.szgl.service.ShouZhiService;

public class ShouZhiAction extends BaseAction {
	//获得收支管理服务类
	private ShouZhiService shouZhiService = (ShouZhiService) getService("shouZhiService");
	//项目的显示页面
	public ActionForward szglInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("shouZhiView");
	}
	//项目的显示页面
	public ActionForward tongJi(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception 
	{
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		java.lang.String prj_sn=dto.getAsString("prj_sn");
		List list = g4Reader.queryForList("szglDemo.tongJi",prj_sn);
		this.setSessionAttribute(request, "list",list);
		request.setAttribute("xmlString",getXml((Dto)list.get(0)));
		request.setAttribute("prj_no",dto.getAsString("prj_no"));
		return mapping.findForward("tongJi");
	}
	//统计项目的费用
	public ActionForward queryMoneyByPrjsn(ActionMapping mapping,ActionForm form,HttpServletRequest request,
			HttpServletResponse response) throws Exception
	{
		List list=(List)this.getSessionAttribute(request, "list");
		this.write(JsonHelper.encodeList2PageJson(list, list.size(),null), response);
		return null;
	}
	//查询统计数据并格式化成xml用于显示图表
	private String getXml(Dto dto)
	{
		//实例化一个图形配置对象
		GraphConfig graphConfig = new GraphConfig();
		//主标题
		graphConfig.setCaption("项目费用一览（单位：元）");
		//X坐标轴名称
		graphConfig.setXAxisName("费用类型");
		//数字值前缀
		//graphConfig.setNumberPrefix("$");
		List dataList = new ArrayList();
			Set set1 = new Set();	
			set1.setName("预算金额"); //名称
			set1.setValue(String.valueOf(dto.getAsBigDecimal("yusuanjia").floatValue())); //数据值
			dataList.add(set1);
			Set set2 = new Set();	
			set2.setName("结算金额"); //名称
			set2.setValue(String.valueOf(dto.getAsBigDecimal("jiesuanjia").floatValue())); //数据值
			dataList.add(set2);
			Set set3 = new Set();	
			set3.setName("已付款"); //名称
			set3.setValue(String.valueOf(dto.getAsBigDecimal("yifukuan").floatValue())); //数据值
			dataList.add(set3);
			Set set4 = new Set();	
			set4.setName("未付款"); //名称
			set4.setValue(String.valueOf(dto.getAsBigDecimal("weifukuan").floatValue())); //数据值
			dataList.add(set4);
		//将图表数据转为Flash能解析的XML资料格式
		String xmlString = FcfDataMapper.toFcfXmlData(dataList, graphConfig);
		return xmlString;
	}
	/**
	 * 查询项目
	 * 
	 */
	public ActionForward queryMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("提示：进入到收支上报查询逻辑。。。。。。");
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		List list = g4Reader.queryForPage("szglDemo.queryCatalogsForPrint", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, (Integer)this.g4Reader.queryForObject("szglDemo.queryCatalogsForPrintCount",dto), G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	//初始化收支管理页面！
	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		Dto exist=(Dto)this.g4Reader.queryForObject("szglDemo.existPrj_sn",dto.getAsString("prj_sn"));
		//倒换页面时查看当前项目是否已经被删除了
		if(exist==null||exist.getAsString("prj_sn")==null)
		{
			request.setAttribute("tip", "提示：对不起，此项目被他人已经删除，已经为你刷新了数据...");
			return mapping.findForward("shouZhiView");
		}
		else
		{
			//由于这里的所有操作只针对一个项目，因此可以当前的项目编号存放在会话中进行长会话操作！
			this.setSessionAttribute(request, "prj_sn",dto.getAsString("prj_sn"));
			
			
			System.out.println("提示：正在转换页面。。。。。。项目编号为："+dto.getAsString("prj_sn"));
			//得到根节点菜单的名字！通过菜单的编号，这个编号是唯一的！
			Dto pdto = (Dto)g4Reader.queryForObject("szglDemo.queryAdminName", "010201");
			//将根节点名字保存起来！
			request.setAttribute("rootMenuName", pdto.getAsString("menuname"));
			request.setAttribute("prj_no",dto.getAsString("prj_no"));
			return mapping.findForward("main");
		}
	}
	//加载根节点下的所有子节点！
	public ActionForward queryMenuItems(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("正在加载费用类型下的所有子节点。。。。。。。。。。。。父节点编号是："+request.getParameter("node"));
		//得到当前传递过俩的节点的编号！一定是个节点而不是叶子，否则不会调用这个方法！因此可以断定传来的节点编号一定是个父编号，用于加载他下面的子编号，子编号也有可能是节点！然后再次调用这个方法一直到加载到叶子
		String parentId=request.getParameter("node");
		//注意这里查询的条件，仅仅只有父节点的编号没有其它条件！
		Dto queryChild=new BaseDto();
		queryChild.put("parentid",parentId);
		List list=this.g4Reader.queryForList("szglDemo.queryChildByAdmin",queryChild);
		//什么一个变量用于接收便利出来的记录！
		Dto menuDto =null;
		//对记录进行过滤，将其值变成树菜单可以认识的值！
		for (int i = 0; i < list.size(); i++) 
		{
			menuDto = (BaseDto) list.get(i);
			if (menuDto.getAsString("leaf").equals(ArmConstants.LEAF_Y))
			{
				menuDto.put("leaf", new Boolean(true));
			}
			else
			{
				menuDto.put("leaf", new Boolean(false));
			}
			if(menuDto.getAsString("id").length()==4)
			{
				menuDto.put("expanded",new Boolean(true));
			}
		}
		//将集合解析成json类型!
		write(JsonHelper.encodeObject2Json(list), response);
		return null;
	}
	//创建添加收支的方法
	public synchronized ActionForward saveShouZhi(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("提示：正在添加收支信息。。。。。。。");
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		//将当前的会话项目id添加到参数中去！
		inDto.put("prj_sn",this.getSessionAttribute(request,"prj_sn"));
		String time=inDto.getAsString("detail_date");
		if(time==null||time.trim().equals(""))
		{
			inDto.put("detail_date",new Date());
		}
		else
		{
			inDto.put("detail_date",inDto.getAsTimestamp("detail_date"));
		}
		System.out.println("=================================="+inDto.getAsString("attc_tp"));
		shouZhiService.addShouZhi(inDto);
		setOkTipMsg("收支消息添加成功", response);
		return mapping.findForward(null);
	}
	//查看当前项目中的所有收支信息！
	public ActionForward shouZhiLook(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		System.out.println("提示：当前正在查询数据，查询出的数据从"+dto.getAsString("start")+"到"+dto.getAsString("limit"));	
		
		//一旦进入了操作附件的界面那么在session作用域中一定会存在一个名为prj_sn的属性，这个属性在跳转到当前项目附件管理模块时添加进去的，是个长会话！
		dto.put("prj_sn",this.getSessionAttribute(request, "prj_sn"));
		
		//默认情况加载当前项目编号下的所有附件信息！（仅仅是第一次进入时才是这样），但下次激发这个方法时可能就包含文件名啊，附件类型编号啊，等这些查询信息！
		List list=this.g4Reader.queryForPage("szglDemo.queryShouZhi",dto);
		System.out.println("传递过来的参数有："+dto.toString());
		System.out.println("当前你要项目的id是："+this.getSessionAttribute(request, "prj_sn"));
		this.write(JsonHelper.encodeList2PageJson(list, (Integer)this.g4Reader.queryForObject("szglDemo.queryShouZhiCount", dto),null), response);
		return null;
	}
	
	//查看当前项目中的所有附件信息！
	public synchronized ActionForward updateShouZhi(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("提示：正在修改收支信息。。。。。。。");
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		inDto.put("prj_sn",this.getSessionAttribute(request,"prj_sn"));
		//处理修改时不填写备注的情况下不会覆盖以前的备注！
		String mark=inDto.getAsString("detail_mark");
		if(mark==null||mark.trim().equals(""))
		{
			inDto.put("detail_mark"," ");
		}
		inDto.put("detail_date",inDto.getAsTimestamp("detail_date"));
		this.shouZhiService.updateShouZhi(inDto);
		this.setOkTipMsg("修改成功！", response);
		return null;
	}
	//创建删除收支的方法
	public ActionForward deleteShouZhi(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("提示：正在删除收支信息。。。。。。。");
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		String[] strChecked = dto.getAsString("strChecked").split(",");
		Dto delete=new BaseDto();
		//由于所有的删除都是针对同一个项目因此下面这个参数可以在循环外设置
		delete.put("prj_sn",this.getSessionAttribute(request, "prj_sn"));
		//循环覆盖，虚幻删除！
		for (int i = 0; i < strChecked.length; i++) 
		{
			delete.put("detail_sn",strChecked[i]);
			this.shouZhiService.deleteShouZhi(delete);
		}
		setOkTipMsg("收支信息删除成功", response);
		return mapping.findForward(null);
	}
}
